<?php

namespace app\user\controller;

use app\model\UserInfo as ModelUserInfo;

/**
 * 订单相关类
 * Class Common
 * @package app\user\controller
 */
class ResetPassword extends Member
{
    public function _initialize()
    {
        parent::_initialize();
    }


    /**
     * 已登录重置密码 \n
     * URI : /password.reset.password
     * @param :
     *  name          |  type  | null | description
     * ---------------|--------|------|-------------
     *  passport      | string | 必填  | 登录凭证
     *  password      | string | 必填  | 旧密码
     *  newPassword   | string | 必填  | 新密码
     *
     * @return :
     *  name        |  type  | description
     * -------------|--------|-------------
     *  ----------- | ------ | 无
     */
    public function resetPassword()
    {
        $str_password = $this->_getContentParams('password'); // 旧密码
        _toValidate('login_pwd', $str_password, 19027);
        $str_new_password = $this->_getContentParams('newPassword'); //新密码
        _toValidate('login_pwd', $str_new_password, 19025);
        $obj = new \app\logic\LoginPassword($this->str_module_name);
        $bool_res = $obj->setLoginPwdWithOldPwd($this->user_id, $str_new_password, $str_password);
        if ($bool_res)  _returnData();
        _returnError(10011);
    }

    /**
     * 获取头像和名称 \n
     * URI : /password.name.head
     * @param :
     *  name          |  type  | null | description
     * ---------------|--------|------|-------------
     *  passport      | string | 必填  | 登录凭证
     *
     * @return :
     *  name         |  type  | description
     * --------------|--------|-------------
     * headPortrait  | string | 头像
     * nickname      | string | 昵称
     * mobile      | string | 手机号
     * isSetLoginPwd | int    | 用户是否设置密码 0无  1有
     *
     */
    public function nameHead()
    {
        $arr_data_info = ModelUserInfo::getInstance()->toFind(['user_id' => $this->user_id], 'head_portrait AS headPortrait,nickname,mobile,password,is_auth as authState,remark');
        $arr_data['headPortrait'] = $arr_data_info['headPortrait'];
        $arr_data['nickname'] = $arr_data_info['nickname'];
        $arr_data['mobile'] = $arr_data_info['mobile'];
        $arr_data['authState'] = $arr_data_info['authState'];
        $arr_data['remark'] = $arr_data_info['remark'];
        if (empty($arr_data_info['password'])){
            $arr_data['isSetLoginPwd'] = 0;
        }else{
            $arr_data['isSetLoginPwd'] = 1;

        }
        _returnData($arr_data);
    }

    /*
     * 首次设置登录密码
     * URI：/user.password.set
     */
    public function firstPwdSet()
    {
        $password = $this->_getContentParams('password');
        _toValidate('login_pwd', $password,19025);

        $obj = new \app\logic\LoginPassword($this->str_module_name);
        $bool_res = $obj->setLoginPwdFirst($this->user_id,$password);
        if ($bool_res) _returnData();
        _returnError(10011);
    }
}
